package com.icim.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Bis_data;

@Repository
public class BisDataDao {
    @Resource
    private JdbcTemplate icimJdbcTemplate;

    public List<Bis_data> listData(String data_cate, String data_id, String data_ext, String data_item, String ext_1,
            String ext_2, String ext_3, String ext_4, String ext_5) {

        StringBuffer sql = new StringBuffer("SELECT * FROM BIS_DATA WHERE 1=1  ");

        List<String> argList = new ArrayList<String>();

        if (StringUtils.isEmpty(data_cate)) {
            throw new RuntimeException("data_cate不能为空");
        } else {
            sql.append(" AND DATA_CATE = ? ");
            argList.add(data_cate);
        }
        if (!StringUtils.isEmpty(data_id)) {
            argList.add(data_id);
            sql.append(" AND DATA_ID = ?");
        }
        if (!StringUtils.isEmpty(data_ext)) {
            argList.add(data_ext);
            sql.append(" AND DATA_EXT = ?");
        }
        if (!StringUtils.isEmpty(data_item)) {
            argList.add(data_item);
            sql.append(" AND DATA_ITEM = ?");
        }
        if (!StringUtils.isEmpty(ext_1)) {
            argList.add(ext_1);
            sql.append(" AND EXT_1 = ? ");
        }
        if (!StringUtils.isEmpty(ext_2)) {
            argList.add(ext_2);
            sql.append(" AND EXT_2 = ? ");
        }
        if (!StringUtils.isEmpty(ext_3)) {
            argList.add(ext_3);
            sql.append(" AND EXT_3 = ?");
        }
        if (!StringUtils.isEmpty(ext_4)) {
            argList.add(ext_4);
            sql.append(" AND EXT_4 = ?");
        }
        if (!StringUtils.isEmpty(ext_5)) {
            argList.add(ext_5);
            sql.append(" AND EXT_5 = ?");
        }

        return icimJdbcTemplate.query(sql.toString(), argList.toArray(), new RowMapper<Bis_data>() {

            @Override
            public Bis_data mapRow(ResultSet rs, int rowNum) throws SQLException {
                Bis_data bisData = new Bis_data();
                bisData.setData_cate(rs.getString("DATA_CATE"));
                bisData.setData_id(rs.getString("DATA_ID"));
                bisData.setData_ext(rs.getString("DATA_EXT"));
                bisData.setData_item(rs.getString("DATA_ITEM"));
                bisData.setExt_1(rs.getString("EXT_1"));
                bisData.setExt_2(rs.getString("EXT_2"));
                bisData.setExt_3(rs.getString("EXT_3"));
                bisData.setExt_4(rs.getString("EXT_4"));
                bisData.setExt_5(rs.getString("EXT_5"));
                bisData.setData_desc(rs.getString("DATA_DESC"));
                return bisData;
            }

        });
    }
}
